using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace ShipkaDefenders
{
    public enum ScreenState { Title, Menu, Game }
    public class AnaLogic : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        public ScreenState State;
        public Screens.Screen Screen;
        public TimeSpan Elapsed;
        public TimeSpan UpdateTime;
        public TimeSpan ScreenTime;
        public Dictionary<string, Texture2D> MapTextures = new Dictionary<string, Texture2D>();
        public Dictionary<string, SpriteFont> MapFonts = new Dictionary<string,SpriteFont>();
        public Gameplay.PlayerData[] PlyData;
        public Gameplay.GameEngine Engine
        {
            get
            {
                return (Screen as Gameplay.GameEngine);
            }
        }
        public AnaLogic()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            TargetElapsedTime = TimeSpan.FromTicks(333333);
        }
        protected override void Initialize()
        {
            ChangeState(ScreenState.Title);
            graphics.IsFullScreen = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 480;
            TouchPanel.EnabledGestures = GestureType.DoubleTap | GestureType.Flick | GestureType.FreeDrag | GestureType.Hold | GestureType.Pinch | GestureType.PinchComplete | GestureType.Tap;
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            MapTextures = Content.LoadContent<Texture2D>("Textures", "project_splash_1", "box", "turk_fight", "turk_walk", "turk_scout_walk", "banner", "banner_anim",
                "range", "new_empty", "new_russian", "new_russian_sniper", "russian_idle", "russian_fight", "russian_dead", "side_bar", "russian_sniper_idle",
                "russian_sniper_fight", "russian_sniper_dead", "gui_ffw", "gui_navbar", "gui_navbar_build_title", "gui_pp");
            MapFonts = Content.LoadContent<SpriteFont>("Fonts", "Segoe16", "Rockwell24", "Calibri18");
            //PlyData = Content.Load<Gameplay.PlayerData[]>("PlayerClasses");
            //Engine.deb = string.Empty;
        }
        protected override void UnloadContent(){}
        protected override void Update(GameTime gameTime)
        {
            Elapsed = gameTime.TotalGameTime;
            UpdateTime = gameTime.ElapsedGameTime;
            while (TouchPanel.IsGestureAvailable)
            {
                Screen.Gestures(gameTime.TotalGameTime, TouchPanel.ReadGesture());
            }
            string ST = string.Empty;
            if(Screen != null) ST = Screen.GetType().Name;
            switch (State)
            {
                case ScreenState.Title:
                    if(ST != "ScreenTitle")
                    Screen = new Screens.ScreenTitle(this);
                    break;
                case ScreenState.Menu:
                    if (ST != "MenuMain")
                    Screen = new Screens.MenuMain(this);
                    break;
                case ScreenState.Game:
                    if (ST != "GameEngine")
                    Screen = new Gameplay.GameEngine(this);
                    break;
            }
            Screen.Update(gameTime.TotalGameTime, gameTime.ElapsedGameTime);
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            Screen.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
        public void ChangeState(ScreenState ss)
        {
            ScreenTime = TimeSpan.Zero;
            State = ss;
        }
        public void WriteString(string font, int size, string text)
        {
            WriteString(font, size, text, Vector2.Zero, Color.Red);
        }
        public void WriteString(string font, int size, string text, Vector2 p, Color c)
        {
            spriteBatch.DrawString(MapFonts[string.Format("{0}{1}", font, size)], text, p, c);
        }
    }
    public static class Ext
    {
        public static Dictionary<String, T> LoadContent<T>(this ContentManager contentManager, string contentFolder, params string[] arr)
        {
            Dictionary<String, T> result = new Dictionary<String, T>();
            foreach (string key in arr)
            {
                result[key] = contentManager.Load<T>(contentFolder + "/" + key);
            }
            return result;
        }
        public static Point Vector2Point(Vector2 v)
        {
            return new Point((int)v.X, (int)v.Y);
        }
    }
    public struct Circle
    {
        private Vector2 v;
        private Vector2 direction;
        private float distanceSquared;
        public Vector2 Center;
        public float Radius;
        public Circle(Vector2 position, float radius)
        {
            this.distanceSquared = 0f;
            this.direction = Vector2.Zero;
            this.v = Vector2.Zero;
            this.Center = position;
            this.Radius = radius;
        }
        public bool Intersects(Rectangle rectangle)
        {
            this.v = new Vector2(MathHelper.Clamp(Center.X, rectangle.Left, rectangle.Right), MathHelper.Clamp(Center.Y, rectangle.Top, rectangle.Bottom));
            this.direction = Center - v;
            this.distanceSquared = direction.LengthSquared();
            return ((distanceSquared > 0) && (distanceSquared < Radius * Radius));
        }
    }
}
